package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.x.special.dao.inter.LeanToValueDao;
import com.ewili.x.special.entity.LeanToValue;
import com.ewili.x.special.service.data.inter.LeanToValueService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;

/**
 * @author wonder
 */
@Service
public class LeanToValueServiceImpl implements LeanToValueService {

    @Autowired
    private LeanToValueDao leanToValueDao;

    @Override
    public void spide() {
        for (int i = 1; i < 10000; i++) {
            try {
                String url = "https://eq.10jqka.com.cn/equityPledgeInfo/index.php?op=getList&sortBy=pledgeRatio&sortType=desc&type=1&pageNum=" + i;
                boolean shouldBreak = this.saveData(url);
                if (shouldBreak) {
                    break;
                }
            } catch (Exception e) {
                ExceptionUtils.propagate(e);
            }
        }

    }

    public boolean saveData(String url) {

        Connection connection = Jsoup.connect(url).timeout(30000000);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }

        if (!content.contains("stockName")) {
            return true;
        }

        Date lastUpdateTime = DateUtils.currentDate(DateUtils.yyyy_MM_dd);

        JSONObject jsonObject = JSON.parseObject(content);
        jsonObject = jsonObject.getJSONObject("data");
        JSONArray jsonArray = jsonObject.getJSONArray("pageList");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String stockCode = object.getString("stockCode");
            //质押比率
            Double pledgeRatio = object.getDouble("pledgeRatio");
            //质押股占持股比率
            Double pledgeRatioOnHoldNum = object.getDouble("pledgeRatioOnHoldNum");
            //今年涨幅
            Double yearRise = object.getDouble("yearRise");
            //质押人
            String pledgePerson = object.getString("pledgePerson");
            //风险等级
            Integer riskLevel = object.getInteger("riskLevel");

            LeanToValue leanToValue = leanToValueDao.findByCode(stockCode);

            if (leanToValue == null) {
                leanToValue = new LeanToValue();
            }
            leanToValue.setCode(stockCode);
            leanToValue.setPledgePerson(pledgePerson);
            leanToValue.setUpdateDate(lastUpdateTime);
            leanToValue.setPledgeRatio(pledgeRatio);
            leanToValue.setPledgeRatioOnHoldNum(pledgeRatioOnHoldNum);
            leanToValue.setYearRise(yearRise);
            leanToValue.setRiskLevel(riskLevel);

            leanToValueDao.save(leanToValue);

        }

        return false;
    }
}
